package game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.config.exception.CustomException;
import common.controller.base.BaseController;
import common.model.enums.ClassificationTypeEnum;
import common.model.po.GBuilding;
import common.model.po.GBuildingLandform;
import common.model.po.GLandform;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import common.service.CommonGBuildingLandformService;
import game.model.converter.GLandformConverter;
import game.model.request.SaveBuildingRequest;
import game.model.request.SearchBuildingRequest;
import game.model.vo.SearchBuildingVo;
import game.service.GBuildingService;
import game.service.GClassificationService;
import game.service.GLandformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 建筑（包括生产机器、物流、照明、墙壁等）前端控制器
 *
 * @author 米泽鹏
 * @since 2022-02-26
 */
@Api(tags = "建筑管理")
@RestController
@RequestMapping("/game/g-building")
public class GBuildingController extends BaseController {

	@Resource
	private GBuildingService buildingService;
	@Resource
	private GClassificationService classificationService;
	@Resource
	private GLandformService landformService;
	@Resource
	private CommonGBuildingLandformService buildingLandformService;

	@ApiOperation(value = "建筑分页查询", notes = "建筑分页查询")
	@PostMapping("/readOnly/searchBuildingByPage")
	public Result<Page<SearchBuildingVo>> searchBuildingByPage(@ApiParam(value = "分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchBuildingRequest, GBuilding> paginationRequest) {
		return successWithData(buildingService.searchBuildingByPage(paginationRequest));
	}

	@ApiOperation(value = "获取全部分类（级联选择器）", notes = "获取全部分类（级联选择器）")
	@PostMapping("/readOnly/getBuildingCascadeClassification")
	public Result<?> getBuildingCascadeClassification() {
		return successWithData(classificationService.getCascadeClassification(ClassificationTypeEnum.BUILDING));
	}

	@ApiOperation(value = "单个建筑查询", notes = "单个建筑查询")
	@GetMapping("/readOnly/getBuildingDetailById/{buildingId}")
	public Result<?> getBuildingDetailById(@ApiParam(value = "建筑id", required = true) @PathVariable Long buildingId) {
		return successWithData(buildingService.getBuildingDetailById(buildingId));
	}

	@ApiOperation(value = "查询所有地貌", notes = "查询所有地貌")
	@GetMapping("/readOnly/getAllLandform")
	public Result<?> getAllLandform() {
		return successWithData(landformService.list());
	}

	@ApiOperation(value = "建筑新增或更新", notes = "建筑新增或更新")
	@PostMapping("/saveBuilding")
	public Result<?> saveBuilding(@ApiParam(value = "建筑新增或更新请求", required = true) @RequestBody @Validated SaveBuildingRequest request) {
		if (request.getNeedPower() == 1 && request.getPower() == null) {
			throw new CustomException("功率不能为空");
		}
		if (request.getIsRequireLandform() == 1 && request.getLandformList().isEmpty()) {
			throw new CustomException("地貌不能为空");
		}
		if (request.getNeedPower() == 0) {
			request.setPower(BigDecimal.ZERO);
		}
		if (request.getIsRequireLandform() == 0) {
			request.getLandformList().clear();
		}
		return buildingService.saveBuilding(request);
	}

	@ApiOperation(value = "建筑删除", notes = "建筑删除")
	@DeleteMapping("/deleteBuilding/{id}")
	public Result<?> deleteBuilding(@ApiParam(value = "建筑id", required = true) @PathVariable Long id) {
		return buildingService.deleteBuilding(id);
	}

	@ApiOperation(value = "根据建筑id查询所有地貌", notes = "根据建筑id查询所有地貌")
	@GetMapping("/readOnly/getAllLandformByBuildingId/{buildingId}")
	public Result<?> getAllLandformByBuildingId(@ApiParam(value = "建筑id", required = true) @PathVariable Long buildingId) {
		List<GBuildingLandform> buildingLandformList = buildingLandformService.lambdaQuery().eq(GBuildingLandform::getBuildingId, buildingId).list();
		List<GLandform> landformList = buildingLandformList.stream().map(GLandformConverter::buildingLandformPoToPo).collect(Collectors.toList());
		return successWithData(landformList);
	}

}
